package com.zzh.lib.views.banner;

import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import com.zzh.lib.views.def.IIndicator;
import com.zzh.lib.views.def.IndicatorSlideMode;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

/**
 * Created by ZZH on 2023/2/16
 *
 * @date: 2023/2/16 08:54
 * @email: zzh_hz@126.com
 * @author: zzh
 * @description:
 */
public class BaseIndicatorView extends View implements IIndicator {

    public static final long DEFAULT_ANIMATION_DURATION = 200L;
    public ValueAnimator valueAnimator = ValueAnimator.ofFloat(-1f, 1f);
    protected Context context;

    protected IndicatorOptions mIndicatorOptions;

    private ViewPager mViewPager = null;
    private ViewPager2 mViewPager2 = null;

    private ViewPager2.OnPageChangeCallback mOnPageChangeCallback = new ViewPager2.OnPageChangeCallback() {
        public void onPageScrolled(int
                                           position,
                                   float positionOffset, int
                                           positionOffsetPixels
        ) {
            BaseIndicatorView.this.onPageScrolled(position, positionOffset, positionOffsetPixels);
        }

        public void onPageSelected(int position) {
            BaseIndicatorView.this.onPageSelected(position);
        }

        public void onPageScrollStateChanged(int state) {
            BaseIndicatorView.this.onPageScrollStateChanged(state);
        }
    };

    public BaseIndicatorView(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        mIndicatorOptions = new IndicatorOptions();
        valueAnimator.setDuration(DEFAULT_ANIMATION_DURATION);
    }

    public BaseIndicatorView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public BaseIndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public BaseIndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }


    public void onPageSelected(int position) {
        if (getSlideMode() == IndicatorSlideMode.NORMAL) {
            setCurrentPosition(position);
            setSlideProgress(0f);
            invalidate();
        }
    }

    public void onPageScrolled(
            int position,
            float positionOffset,
            int positionOffsetPixels
    ) {
        if (getSlideMode() != IndicatorSlideMode.NORMAL && getPageSize() > 1) {
            scrollSlider(position, positionOffset);
            invalidate();
        }
    }

    private void scrollSlider(
            int position,
            float positionOffset
    ) {
        if (mIndicatorOptions.getSlideMode() == IndicatorSlideMode.SCALE
                || mIndicatorOptions.getSlideMode() == IndicatorSlideMode.COLOR
        ) {
            setCurrentPosition(position);
            setSlideProgress(positionOffset);
        } else {
            if (position % getPageSize() == getPageSize() - 1) { //   最后一个页面与第一个页面
                if (positionOffset < 0.5) {
                    setCurrentPosition(position);
                    setSlideProgress(0f);
                } else {
                    setCurrentPosition(0);
                    setSlideProgress(0f);
                }
            } else {    //  中间页面
                setCurrentPosition(position);
                setSlideProgress(positionOffset);
            }
        }
    }

    @Override
    public void notifyDataChanged() {
        setupViewPager();
        requestLayout();
        invalidate();
    }

    private void setupViewPager() {
        if (mViewPager != null) {
            mViewPager.removeOnPageChangeListener(this);
            mViewPager.addOnPageChangeListener(this);
            if (mViewPager.getAdapter() != null) {
                setPageSize(mViewPager.getAdapter().getCount());
            }
        }

        if (mViewPager2 != null) {
            mViewPager2.unregisterOnPageChangeCallback(mOnPageChangeCallback);
            mViewPager2.registerOnPageChangeCallback(mOnPageChangeCallback);
            if (mViewPager2.getAdapter() != null) {
                setPageSize(mViewPager2.getAdapter().getItemCount());
            }
        }
    }

    public float getNormalSlideWidth() {
        return mIndicatorOptions.getNormalSliderWidth();
    }

    public void setNormalSlideWidth(float normalSliderWidth) {
        mIndicatorOptions.setNormalSliderWidth(normalSliderWidth);
    }

    public float getCheckedSlideWidth() {
        return mIndicatorOptions.getCheckedSliderWidth();
    }

    public void setCheckedSlideWidth(float checkedSliderWidth) {
        mIndicatorOptions.setCheckedSliderWidth(checkedSliderWidth);
    }

    /*val checkedSliderWidth:

    Float
    get() =mIndicatorOptions.checkedSliderWidth;*/

    public void setCurrentPosition(int currentPosition) {
        mIndicatorOptions.setCurrentPosition(currentPosition);
    }

    public int getCurrentPosition() {
        return mIndicatorOptions.getCurrentPosition();
    }

    public void getIndicatorGap(float indicatorGap) {
        mIndicatorOptions.setSliderGap(indicatorGap);
    }

    public void setIndicatorGap(float indicatorGap) {
        mIndicatorOptions.setSliderGap(indicatorGap);
    }

    public void setCheckedColor(@ColorInt int normalColor) {
        mIndicatorOptions.setCheckedSliderColor(normalColor);
    }

    public int getCheckedColor() {
        return mIndicatorOptions.getCheckedSliderColor();
    }

    public void setNormalColor(@ColorInt int normalColor) {
        mIndicatorOptions.setNormalSliderColor(normalColor);
    }

    public float getSlideProgress() {
        return mIndicatorOptions.getSlideProgress();
    }

    public void setSlideProgress(float slideProgress) {
        mIndicatorOptions.setSlideProgress(slideProgress);
    }

    public int getPageSize() {
        return mIndicatorOptions.getPageSize();
    }

    public BaseIndicatorView setPageSize(int pageSize) {
        mIndicatorOptions.setPageSize(pageSize);
        return this;
    }

    public BaseIndicatorView setSliderColor(
            @ColorInt int normalColor,
            @ColorInt int selectedColor
    ) {
        mIndicatorOptions.setSliderColor(normalColor, selectedColor);
        return this;
    }

    public BaseIndicatorView setSliderWidth(float sliderWidth) {
        mIndicatorOptions.setSliderWidth(sliderWidth);
        return this;
    }

    public BaseIndicatorView setSliderWidth(
            float normalSliderWidth,
            float selectedSliderWidth
    ) {
        mIndicatorOptions.setSliderWidth(normalSliderWidth, selectedSliderWidth);
        return this;
    }

    public BaseIndicatorView setSliderGap(float sliderGap) {
        mIndicatorOptions.setSliderGap(sliderGap);
        return this;
    }

    public int getSlideMode() {
        return mIndicatorOptions.getSlideMode();
    }

    public BaseIndicatorView setSlideMode(int slideMode) {
        mIndicatorOptions.setSlideMode(slideMode);
        return this;
    }

    public BaseIndicatorView setIndicatorStyle(int indicatorStyle) {
        mIndicatorOptions.setIndicatorStyle(indicatorStyle);
        return this;
    }

    public BaseIndicatorView setSliderHeight(float sliderHeight) {
        mIndicatorOptions.setSliderHeight(sliderHeight);
        return this;
    }

    public void setupWithViewPager(ViewPager viewPager) {
        mViewPager = viewPager;
        notifyDataChanged();
    }

    public void setupWithViewPager(ViewPager2 viewPager2) {
        mViewPager2 = viewPager2;
        notifyDataChanged();
    }

    public void showIndicatorWhenOneItem(boolean showIndicatorWhenOneItem) {
        mIndicatorOptions.showIndicatorOneItem = showIndicatorWhenOneItem;
    }

    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public void setIndicatorOptions(IndicatorOptions options) {
        mIndicatorOptions = options;
    }
}
